package cn.zktr.Personnel.common;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeStamp {
	
	public Timestamp getNetworkTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
        try { 
            URL url=new URL("http://www.baidu.com");
            URLConnection conn=url.openConnection();
            conn.connect();
        	Timestamp nousedate = new Timestamp(format.parse(format.format(conn.getDate())).getTime());
            return nousedate;
        }catch (MalformedURLException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }catch (ParseException e) {
			e.printStackTrace();
		} 
        return null;
	}
	public Timestamp byTimestamp(String card) {
        //字符串截取下标从0开始的
        String sr1=card.substring(6, 10);
        String sr2=card.substring(10, 12);
        String sr3=card.substring(12, 14);
        String sr = sr1+"-"+sr2+"-"+sr3;
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        format.setLenient(false);
        try {
        	Timestamp ts = new Timestamp(format.parse(sr).getTime());
        	System.out.println(ts.toString());
        	return ts;
        } catch (ParseException e) {
        	e.printStackTrace();
        }
		return null;
	}
	public Timestamp valueOf(String time) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		Timestamp t1 = null;
		try {
			Date date = sf.parse(time);
			System.out.println(date);
			t1 = new Timestamp(date.getTime());
			System.out.println(t1);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return t1;
	}
}
